package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.service.IMenuService;
import com.blb.service.IRoleService;
import com.blb.service.IStaffService;
import com.blb.service.IUserRoleService;
import com.blb.utils.*;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    IStaffService staffService;
    @Autowired
    IUserRoleService userRoleService;
    @Autowired
    IMenuService menuService;
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/list")
    public DataTablesPage list(Integer draw, Integer start, Integer length){
        System.out.println(draw);
        System.out.println(start);
        System.out.println(length);

        List<Role> list = iRoleService.getPageRole(start, length, ShiroUtils.getCompany());
        int count = iRoleService.getPageRoleCount(ShiroUtils.getCompany());
        return new DataTablesPage(draw,count,count, list);
    }
    @RequiresPermissions("role")
    @RequestMapping("/toaddRole")
    public String addRolePage(Model model){
        List<Menu> menus = menuService.getTreeMenu("0");
        model.addAttribute("menus",menus);
        System.out.println(menus);
        return "role/wt_addJiaose";
    }
    /**
     * 修改员工状态
     * @param statues
     * @param id
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/updateStaus")
    public Response updateStaus(String statues ,String id){
        System.out.println(statues);
        System.out.println(id);
        iRoleService.updateStatus(statues,id);
        return Response.success() ;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/addRole")
    public Response addRole(Role role,String[] menuIds){
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        role.setId(UUIDUtils.getId());
        role.setRoleDate(new Date());
        role.setCreateUser(user.getUserAccount());
        role.setCompanyId(user.getCompany().getId());
//        //添加用户角色关联表
//        UserRole userRole = new UserRole();
//        userRole.setId(UUIDUtils.getId());
//        userRole.setUserId(user.getId());
//        userRole.setRoleId(role.getId());
        iRoleService.addRole(role,menuIds);
        return Response.success() ;
    }

    /**
     * 绑定员工
     * @param
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/bindingStaff")
    public Response bindingStaff( ){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Response  respon = new Response();
        List<Staff> staffs = staffService.selectStaffBycompanyId(user.getCompany().getId());
//        List<Staff> staffs =staffService.list();
        respon.setData(staffs);
        System.out.println(respon);
        return respon ;
    }

    /**
     * 添加员工角色信息
     * @param staffIds
     * @param roleId
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/addStaffRole")
    public Response addStaffRole(@RequestParam(value = "staffIds[]")String[] staffIds,String roleId){
        System.out.println(roleId);
        staffService.addStaffRole(roleId,staffIds);
        return Response.success() ;
    }

    /**
     * 修改角色页面
     * @param id
     * @return
     */
    @RequiresPermissions("role")
    @RequestMapping("/toupdateRole")
    public String toupdateRole(String id ,Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Role role = iRoleService.selectOneRole(id, user.getCompany().getId());
        System.out.println(role);
        model.addAttribute("role",role);
        List<Menu> menus = menuService.getTreeMenu("0");
        model.addAttribute("menus",menus);
        System.out.println(menus);
        return "role/wt_updateJiaose";
    }
    /**
     * 修改角色
     * @param role
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/updateRole")
    public Response updateRole(Role role,String id,String[] menuIds){

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        role.setUpdateTime(new Date());
        role.setUpdateUser(user.getUserAccount());
        iRoleService.updateRole(role,id,menuIds);
        return Response.success() ;
    }
    /**
     * 单个删除
     * @param roleId
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/deleteRole")
    public Response deleteRole( String roleId){
        iRoleService.deleteRole(roleId);
        return Response.success() ;
    }

    /**
     * 批量删除
     * @param roleIds
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/deleteRoles")
    public Response deleteRoles(@RequestParam(value = "roleIds[]") String[] roleIds){
        System.out.println(roleIds);
        for (String roleId:roleIds){
            iRoleService.deleteRole(roleId);
        }
        return Response.success() ;
    }

    /**
     * 模糊查询
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/like")
    public DataTablePage like(int start, int length, String roleName){
        System.out.println(roleName);
        Page<Role> page = new Page<>(start,length);
        List<Role> roles = iRoleService.selectRoleByRoleName(roleName);
        //总条数
        page.setRecords(roles);
        //大小
        page.setTotal(roles.size());
        return new DataTablePage<Role>( page.getTotal(), page.getTotal(), page.getRecords());
    }
    @RequiresPermissions("role")
    @ResponseBody
    @PostMapping("/roleExist")
    public Response roleExist(String roleName){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Role role = iRoleService.roleNameisExist(roleName, user.getCompany().getId());
        if (role!=null){
            return Response.error("角色已存在") ;
        }
        return Response.success();
    }
}

